package com.example.demo.controller;


import com.example.demo.entity.Student;
import com.example.demo.entity.Users;
import com.example.demo.service.ClassesService;
import com.example.demo.service.StudentService;
import com.example.demo.service.UsersService;
import com.example.demo.service.XuanKeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private ClassesService classesService;

    @Autowired
    private XuanKeService xuanKeService;



    @ResponseBody
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Map<String, Object> searchStudents(@RequestBody Map<String, Object> params) {
        return studentService.searchStudents(params);
    }



    @GetMapping("/classes")
    @ResponseBody
    public Map<String, Object> getUser(@RequestParam("username") String username) {
        Map<String, Object> retData = new HashMap<>();
        if (!username.isEmpty()) {
            Users classId = usersService.getClassByUsername(username);
            retData.put("code", 200);
            retData.put("users", classesService.getClassesById(classId));
        } else {
            retData.put("code", 500);
            retData.put("message", "未找到该用户");
        }
        return retData;
    }

    @GetMapping("/score")
    @ResponseBody
    public Map<String, Object> getByUsersId(@RequestParam("username") String username) {
        Map<String, Object> retData = new HashMap<>();
        if (!username.isEmpty()) {
            Users studentNumber = usersService.getIdByUsername(username);
            retData.put("code", 200);
            retData.put("users", xuanKeService.getByUsersId(studentNumber));
        } else {
            retData.put("code", 500);
            retData.put("message", "未找到该用户");
        }
        return retData;
    }


    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Map<String, Object> deleteStudent(@RequestBody Map<String, Object> params) {
        return studentService.deleteStudent(params);
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Map<String, Object> updateStudent(@RequestBody Student student) {
        Map<String, Object> retData = new HashMap<>();
        if (student.getUsername().isEmpty() || student.getStudentId().isEmpty()) {
            retData.put("code", 500);
            retData.put("message", "参数错误！");
            return retData;
        } else {
            return studentService.updateStudent(student);
        }
    }

}
